<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="&amp;#x524D;&amp;#x8A00;&amp;#x8BDD;&amp;#x8BF4;&amp;#x65E5;&amp;#x671F;&amp;#x65F6;&amp;#x95F4;&amp;#x9009;&amp;#x62E9;&amp;#x63A7;&amp;#x4EF6;&amp;#x8BB8;&amp;#x591A;&amp;#x9879;&amp;#x76EE;&amp;#x90FD;&amp;#x4F1A;&amp;#x7528;&amp;#x5230;&amp;#xFF0C;&amp;#x4ECA;&amp;#x5929;&amp;#x5C0F;&amp;#x53EF;">
<meta property="og:type" content="article">
<meta property="og:title" content="Android DatePicker">
<meta property="og:url" content="http://yoursite.com/2019/08/21/Android-DatePicker/index.html">
<meta property="og:site_name" content="Silence潇湘夜雨">
<meta property="og:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x8BDD;&amp;#x8BF4;&amp;#x65E5;&amp;#x671F;&amp;#x65F6;&amp;#x95F4;&amp;#x9009;&amp;#x62E9;&amp;#x63A7;&amp;#x4EF6;&amp;#x8BB8;&amp;#x591A;&amp;#x9879;&amp;#x76EE;&amp;#x90FD;&amp;#x4F1A;&amp;#x7528;&amp;#x5230;&amp;#xFF0C;&amp;#x4ECA;&amp;#x5929;&amp;#x5C0F;&amp;#x53EF;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-a1a4549b2b075bd4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-a843b95c6d6c589a.gif?imageMogr2/auto-orient/strip">
<meta property="og:updated_time" content="2019-07-19T05:32:56.134Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android DatePicker">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x8BDD;&amp;#x8BF4;&amp;#x65E5;&amp;#x671F;&amp;#x65F6;&amp;#x95F4;&amp;#x9009;&amp;#x62E9;&amp;#x63A7;&amp;#x4EF6;&amp;#x8BB8;&amp;#x591A;&amp;#x9879;&amp;#x76EE;&amp;#x90FD;&amp;#x4F1A;&amp;#x7528;&amp;#x5230;&amp;#xFF0C;&amp;#x4ECA;&amp;#x5929;&amp;#x5C0F;&amp;#x53EF;">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1716569-a1a4549b2b075bd4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/21/Android-DatePicker/">





  <title>Android DatePicker | Silence潇湘夜雨</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Silence潇湘夜雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Android开发一枚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home //首页"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user //关于"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags  //标签"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th //分类"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive //归档"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar //日程表"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap //站点地图"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/21/Android-DatePicker/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Silence潇湘夜雨">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Silence潇湘夜雨">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android DatePicker</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-21T15:51:43+08:00">
                2019-08-21
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.6k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="&#x524D;&#x8A00;"><a href="#&#x524D;&#x8A00;" class="headerlink" title="&#x524D;&#x8A00;"></a>&#x524D;&#x8A00;</h3><p><strong>&#x8BDD;&#x8BF4;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x9009;&#x62E9;&#x63A7;&#x4EF6;&#x8BB8;&#x591A;&#x9879;&#x76EE;&#x90FD;&#x4F1A;&#x7528;&#x5230;&#xFF0C;&#x4ECA;&#x5929;&#x5C0F;&#x53EF;&#x4E0D;&#x624D;&#xFF0C;&#x4E5F;&#x53D1;&#x4E00;&#x4E2A;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x65E5;&#x671F;&#x9009;&#x62E9;&#x63A7;&#x4EF6;</strong></p>
<p>##&#x5148;&#x4E0A;&#x56FE;</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-a1a4549b2b075bd4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="CC8V2$(JPZ`{WT42ICK7F}K.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-a843b95c6d6c589a.gif?imageMogr2/auto-orient/strip" alt="demo.gif"></p>
<hr>
<h2 id="1&#x3001;&#x9996;&#x5148;&#x81EA;&#x5B9A;&#x4E49;&#x65E5;&#x671F;&#x9009;&#x62E9;&#x5DE5;&#x5177;&#x7C7B;"><a href="#1&#x3001;&#x9996;&#x5148;&#x81EA;&#x5B9A;&#x4E49;&#x65E5;&#x671F;&#x9009;&#x62E9;&#x5DE5;&#x5177;&#x7C7B;" class="headerlink" title="1&#x3001;&#x9996;&#x5148;&#x81EA;&#x5B9A;&#x4E49;&#x65E5;&#x671F;&#x9009;&#x62E9;&#x5DE5;&#x5177;&#x7C7B;"></a>1&#x3001;&#x9996;&#x5148;&#x81EA;&#x5B9A;&#x4E49;&#x65E5;&#x671F;&#x9009;&#x62E9;&#x5DE5;&#x5177;&#x7C7B;</h2><p><strong>&#x91CC;&#x9762;&#x6CE8;&#x91CA;&#x6BD4;&#x8F83;&#x8BE6;&#x7EC6;&#xFF0C;&#x6211;&#x5C31;&#x4E0D;&#x5728;&#x8FD9;&#x91CC;&#x4E00;&#x4E00;&#x8D58;&#x8FF0;&#x4E86;&#x3002;</strong></p>
<pre><code>  /**
    * &#x65E5;&#x671F;&#x65F6;&#x95F4;&#x9009;&#x62E9;&#x63A7;&#x4EF6; &#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#xFF1A; private EditText inputDate;&lt;br&gt;
    * &#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x7684;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x6587;&#x672C;&#x7F16;&#x8F91;&#x6846; private String initDateTime=&quot;2012&#x5E74;9&#x6708;3  &#x65E5; 14:44&quot;,&#x521D;&#x59CB;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x503C;
    * &#x5728;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#xFF1A; inputDate.setOnClickListener(new OnClickListener()     
    */
  public class DatePickerDialogUtil implements OnDateChangedListener,
    OnTimeChangedListener {
/** &#x65E5;&#x671F;&#x9009;&#x62E9; */
private DatePicker mDateChoose;
/** &#x65F6;&#x95F4;&#x9009;&#x62E9; */
private TimePicker mTimeChoose;
/** &#x5BF9;&#x8BDD;&#x6846; */
private AlertDialog mDialog;
/** &#x8BBE;&#x7F6E;&#x65F6;&#x95F4; */
private String mDateTime;
/** &#x521D;&#x59CB;&#x5316;&#x65F6;&#x95F4; */
private String mInitDateTime;
private final SimpleDateFormat sdf = new SimpleDateFormat(
        &quot;yyyy&#x5E74;MM&#x6708;dd&#x65E5; HH:mm:ss&quot;);
/** &#x663E;&#x793A;&#x65F6;&#x95F4; */
private String showDate;
/** Activity&#x5BF9;&#x8C61; */
private final Activity mActivity;

/**
 * &#x65E5;&#x671F;&#x65F6;&#x95F4;&#x5F39;&#x51FA;&#x9009;&#x62E9;&#x6846;&#x51FD;&#x6570;
 * 
 * @param activity
 *            &#x8C03;&#x7528;&#x7236;Activity
 * @param dateTime
 *            &#x521D;&#x59CB;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x503C;&#xFF0C;&#x4F5C;&#x4E3A;&#x5F39;&#x51FA;&#x7A97;&#x53E3;&#x7684;&#x6807;&#x9898;&#x548C;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x521D;&#x59CB;&#x503C;
 */

public DatePickerDialogUtil(Activity activity, String dateTime) {
    this.mActivity = activity;
    this.mInitDateTime = dateTime;
}

/**
 * &#x521D;&#x59CB;&#x5316;&#x65F6;&#x95F4;&#x65E5;&#x671F;
 * 
 * @param datePicker
 *            &#x65E5;&#x671F;
 * @param timePicker
 *            &#x65F6;&#x95F4;
 */
public void init(DatePicker datePicker, TimePicker timePicker) {
    // &#x521D;&#x59CB;&#x5316;Calendar
    Calendar calendar = Calendar.getInstance();
    // datePicker&#x4E0D;&#x7B49;&#x4E8E;&#x7A7A;&#xFF0C;&#x5E76;&#x4E14;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x95F4;&#x4E0D;&#x4E3A;&#x7A7A;
    if (!(datePicker == null || &quot;&quot;.equals(mInitDateTime))) {

    } else {
        mInitDateTime = calendar.get(Calendar.YEAR) + &quot;&#x5E74;&quot;
                + calendar.get(Calendar.MONTH) + &quot;&#x6708;&quot;
                + calendar.get(Calendar.DAY_OF_MONTH) + &quot;&#x65E5;&quot;
                + calendar.get(Calendar.HOUR_OF_DAY) + &quot;:&quot;
                + calendar.get(Calendar.MINUTE);
    }
    // &#x521D;&#x59CB;&#x5316;&#x65F6;&#x95F4;
    mDateChoose.init(calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), this);
    // &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x65F6;&#x95F4;
    mTimeChoose.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
    // &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x5206;&#x949F;
    mTimeChoose.setCurrentMinute(calendar.get(Calendar.MINUTE));
}

/**
 * &#x5F39;&#x51FA;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x9009;&#x62E9;&#x6846;&#x65B9;&#x6CD5;
 * 
 * @param dateText
 *            &#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x7684;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x9009;&#x62E9;&#x6846;
 * @return
 */
public AlertDialog datePickerDialog(final Button dateText) {
    LinearLayout mInflater = (LinearLayout) mActivity.getLayoutInflater()
            .inflate(R.layout.datepicker_dialog, null);
    mDateChoose = (DatePicker) mInflater.findViewById(R.id.date_choose);
    mTimeChoose = (TimePicker) mInflater.findViewById(R.id.time_choose);
    // &#x521D;&#x59CB;&#x5316;&#x65E5;&#x671F;&#x65F6;&#x95F4;
    init(mDateChoose, mTimeChoose);
    // &#x8BBE;&#x7F6E;&#x65E5;&#x671F;&#x4E3A;24&#x5C0F;&#x65F6;&#x5236;
    mTimeChoose.setIs24HourView(true);
    // &#x65F6;&#x95F4;&#x6539;&#x53D8;&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x5668;
    mTimeChoose.setOnTimeChangedListener(this);
    showDate = sdf.format(System.currentTimeMillis());
    // &#x521B;&#x5EFA;&#x5BF9;&#x8BDD;&#x6846;
    mDialog = new AlertDialog.Builder(mActivity).setTitle(mInitDateTime)
            .setView(mInflater)
            .setPositiveButton(&quot;&#x8BBE;&#x7F6E;&quot;, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dateText.setText(mDateTime);
                }
            }).setNegativeButton(&quot;&#x53D6;&#x6D88;&quot;, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dateText.setText(showDate);
                }
            }).show();
    // &#x8BBE;&#x7F6E;&#x65E5;&#x671F;&#x6539;&#x53D8;
    onDateChanged(null, 0, 0, 0);
    return mDialog;
}

/**
 * &#x65F6;&#x95F4;&#x76D1;&#x542C;&#x5668;
 */
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    // &#x8BBE;&#x7F6E;&#x65E5;&#x671F;&#x6539;&#x53D8;
    onDateChanged(null, 0, 0, 0);
}

/**
 * &#x65E5;&#x671F;&#x76D1;&#x542C;&#x5668;
 */
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // &#x83B7;&#x53D6;&#x65E5;&#x5386;&#x5B9E;&#x4F8B;
    Calendar calendar = Calendar.getInstance();
    // &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;
    calendar.set(mDateChoose.getYear(), mDateChoose.getMonth(),
            mDateChoose.getDayOfMonth(), mTimeChoose.getCurrentHour(),
            mTimeChoose.getCurrentMinute());
    // &#x683C;&#x5F0F;&#x5316;&#x5B57;&#x7B26;&#x4E32;
    SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy&#x5E74;MM&#x6708;dd&#x65E5; hh:mm&quot;);
    mDateTime = sdf.format(calendar.getTime());
    mDialog.setTitle(mDateTime);
}

/**
 * &#x622A;&#x53D6;&#x5B57;&#x7B26;&#x4E32;
 * 
 * @param src
 *            &#x6570;&#x636E;&#x6E90;
 * @param pattern
 *            &#x5339;&#x914D;&#x6A21;&#x5F0F;
 * @param indexOrLast
 *            &#x6700;&#x540E;&#x4E00;&#x4E2A;&#x4E0B;&#x6807;
 * @param frontOrBack
 *            &#x53D6;&#x524D;&#x9762;&#x7684;&#x503C;&#x8FD8;&#x662F;&#x540E;&#x9762;&#x7684;&#x503C;
 * @return
 */
public static String spliteString(String src, String pattern,
        String indexOrLast, String frontOrBack) {
    // &#x7ED3;&#x679C;
    String result = &quot;&quot;;
    int location = -1;
    // &#x4E0D;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5F53;&#x524D;&#x4E0B;&#x6807;
    if (indexOrLast.equalsIgnoreCase(&quot;index&quot;)) {
        // &#x53D6;&#x5F97;&#x5B57;&#x7B26;&#x4E32;&#x7B2C;&#x4E00;&#x6B21;&#x51FA;&#x73B0;&#x7684;&#x5730;&#x65B9;
        location = src.indexOf(pattern);
    } else {
        // &#x5B57;&#x7B26;&#x4E32;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x7684;&#x4F4D;&#x7F6E;
        location = src.lastIndexOf(pattern);
    }
    // &#x4E0D;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#xFF0C;&#x5982;&#x679C;&#x5B9E;&#x5728;&#x524D;&#x9762;
    if (frontOrBack.equalsIgnoreCase(&quot;front&quot;)) {
        if (location != -1) {
            // &#x622A;&#x53D6;&#x5B57;&#x7B26;&#x4E32;
            result = src.substring(0, location);
        } else {
            if (location != -1) {
                // &#x622A;&#x53D6;&#x5B57;&#x7B26;&#x4E32;
                result = src.substring(location + 1, src.length());
            }
        }
    }

    return result;
}

/**
 * &#x5B9E;&#x73B0;&#x5C06;&#x521D;&#x59CB;&#x65E5;&#x671F;&#x65F6;&#x95F4;2016&#x5E74;01&#x6708;10&#x65E5; 16:45 &#x62C6;&#x5206;&#x6210;&#x5E74; &#x6708; &#x65E5; &#x65F6; &#x5206; &#x79D2;,&#x5E76;&#x8D4B;&#x503C;&#x7ED9;calendar
 * 
 * @param initDateTime
 *            &#x521D;&#x59CB;&#x65E5;&#x671F;&#x65F6;&#x95F4;&#x503C; &#x5B57;&#x7B26;&#x4E32;&#x578B;
 * @return Calendar
 */
private Calendar getCalendarByInitDate(String initDateTime) {
    // &#x83B7;&#x53D6;&#x65E5;&#x5386;&#x5B9E;&#x4F8B;
    Calendar calendar = Calendar.getInstance();
    /*
     * &#x5C06;&#x521D;&#x59CB;&#x65E5;&#x671F;&#x65F6;&#x95F4;2016&#x5E74;01&#x6708;10&#x65E5; 16:45 &#x62C6;&#x5206;&#x6210;&#x5E74; &#x6708; &#x65E5; &#x65F6; &#x5206; &#x79D2;
     */
    // &#x65E5;&#x671F;
    String date = spliteString(mInitDateTime, &quot;&#x65E5;&quot;, &quot;index&quot;, &quot;front&quot;);
    // &#x65F6;&#x95F4;
    String time = spliteString(mInitDateTime, &quot;&#x65E5;&quot;, &quot;index&quot;, &quot;back&quot;);
    // &#x5E74;&#x4EFD;
    String year = spliteString(date, &quot;&#x5E74;&quot;, &quot;index&quot;, &quot;front&quot;);
    // &#x6708;&#x5206;&#x548C;&#x65E5;&#x671F;
    String monthAndDay = spliteString(date, &quot;&#x5E74;&quot;, &quot;index&quot;, &quot;back&quot;);
    // &#x6708;
    String month = spliteString(monthAndDay, &quot;&#x6708;&quot;, &quot;index&quot;, &quot;front&quot;);
    // &#x65E5;
    String dayOfMonth = spliteString(monthAndDay, &quot;&#x6708;&quot;, &quot;index&quot;, &quot;back&quot;);
    // &#x65F6;
    String hour = spliteString(time, &quot;:&quot;, &quot;index&quot;, &quot;front&quot;);
    // &#x5206;
    String minute = spliteString(time, &quot;:&quot;, &quot;index&quot;, &quot;back&quot;);
    // &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x5E74;&#x4EFD;
    int currentYear = Integer.valueOf(year.trim()).intValue();
    // &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x6708;&#x4EFD;
    int currentMonth = Integer.valueOf(month.trim()).intValue();
    // &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x65E5;
    int currentDay = Integer.valueOf(dayOfMonth.trim()).intValue();
    // &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x65F6;
    int currentHour = Integer.valueOf(hour.trim()).intValue();
    // &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x5206;
    int currentMinute = Integer.valueOf(minute.trim()).intValue();
    // &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x5E74;&#x3001;&#x6708;&#x3001;&#x65E5;&#x3001;&#x65F6;&#x3001;&#x5206;
    calendar.set(currentYear, currentMonth, currentDay, currentHour,
            currentMinute);
    return calendar;
}
}</code></pre><h2 id="2&#x3001;&#x5E03;&#x5C40;&#x6587;&#x4EF6;"><a href="#2&#x3001;&#x5E03;&#x5C40;&#x6587;&#x4EF6;" class="headerlink" title="2&#x3001;&#x5E03;&#x5C40;&#x6587;&#x4EF6;"></a>2&#x3001;&#x5E03;&#x5C40;&#x6587;&#x4EF6;</h2><p><strong>&#x5E03;&#x5C40;&#x91CC;&#x9762;&#x5C31;&#x4E00;&#x4E2A;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;&#x9009;&#x62E9;&#x63A7;&#x4EF6;&#xFF0C;&#x8FD9;&#x91CC;&#x65E0;&#x9700;&#x591A;&#x8A00;&#xFF0C;&#x76F8;&#x4FE1;&#x5404;&#x4F4D;&#x770B;&#x5B98;&#x90FD;&#x80FD;&#x770B;&#x61C2;</strong></p>
<pre><code>  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;center&quot;
android:orientation=&quot;vertical&quot; &gt;
  &lt;DatePicker
    android:id=&quot;@+id/date_choose&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;0dp&quot;
    android:calendarViewShown=&quot;false&quot;
    android:layout_weight=&quot;1&quot; /&gt;

&lt;TimePicker
    android:id=&quot;@+id/time_choose&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;0dp&quot;
    android:layout_weight=&quot;1&quot; /&gt;
&lt;/LinearLayout&gt;</code></pre><h2 id="3&#x3001;&#x5982;&#x4F55;&#x8C03;&#x7528;"><a href="#3&#x3001;&#x5982;&#x4F55;&#x8C03;&#x7528;" class="headerlink" title="3&#x3001;&#x5982;&#x4F55;&#x8C03;&#x7528;"></a>3&#x3001;&#x5982;&#x4F55;&#x8C03;&#x7528;</h2><p><strong>&#x8BF7;&#x770B;&#x5982;&#x4E0B;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;</strong></p>
<pre><code>  public class MainActivity extends Activity implements OnClickListener {
/** &#x5F00;&#x59CB;&#x65F6;&#x95F4; */
private Button mBtnStart;
/** &#x7ED3;&#x675F;&#x65F6;&#x95F4; */
private Button mBtnEnd;
/** &#x8BBE;&#x7F6E;&#x6587;&#x672C;&#x6309;&#x94AE; */
private Button mBtnSetting;
/** &#x8BBE;&#x7F6E;&#x5B57;&#x4F53;&#x6837;&#x5F0F;&#x6309;&#x94AE; */
private Button mBtnSetShaow;
private final SimpleDateFormat sdf = new SimpleDateFormat(
        &quot;yyyy&#x5E74;MM&#x6708;dd&#x65E5; HH:mm:ss&quot;);
/** &#x521D;&#x59CB;&#x5316;&#x5F00;&#x59CB;&#x65F6;&#x95F4; */
protected String mInitDateTimeStart;
/** &#x521D;&#x59CB;&#x5316;&#x7ED3;&#x675F;&#x65F6;&#x95F4; */
protected String mInitDateTimeEnd;
/** DatePickerDialogUtil&#x7C7B; */
private DatePickerDialogUtil mDatePickerDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {

    mInitDateTimeStart = sdf.format(System.currentTimeMillis());
    mInitDateTimeEnd = sdf.format(System.currentTimeMillis());

    mBtnStart = (Button) findViewById(R.id.btn_start);
    mBtnStart.setText(mInitDateTimeStart);
    mBtnStart.setOnClickListener(this);

    mBtnEnd = (Button) findViewById(R.id.btn_end);
    mBtnEnd.setText(mInitDateTimeEnd);
    mBtnEnd.setOnClickListener(this);

    mBtnSetting = (Button) findViewById(R.id.btn_textSet);
    mBtnSetting.setOnClickListener(this);

    mBtnSetShaow = (Button) findViewById(R.id.btn_setShaow);
    mBtnSetShaow.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_start: {// &#x5F00;&#x59CB;&#x65F6;&#x95F4;&#x6309;&#x94AE;

        mDatePickerDialog = new DatePickerDialogUtil(this,
                mInitDateTimeStart);
        mDatePickerDialog.datePickerDialog(mBtnStart);
        break;
    }

    case R.id.btn_end: {// &#x7ED3;&#x675F;&#x65F6;&#x95F4;&#x6309;&#x94AE;
        mDatePickerDialog = new DatePickerDialogUtil(this, mInitDateTimeEnd);
        mDatePickerDialog.datePickerDialog(mBtnEnd);
        break;
    }
    case R.id.btn_textSet: {// &#x8BBE;&#x7F6E;&#x6587;&#x672C;&#x6309;&#x94AE;
        Intent intent = new Intent(this, TextSetActivity.class);
        startActivity(intent);
        break;
    }
    case R.id.btn_setShaow: {// &#x8BBE;&#x7F6E;&#x5B57;&#x4F53;&#x6837;&#x5F0F;&#x6309;&#x94AE;
        Intent intent = new Intent(this, SetTextViewActivity.class);
        startActivity(intent);
        break;
    }
    }
}
}</code></pre><h2 id="&#x7ED3;&#x8BED;"><a href="#&#x7ED3;&#x8BED;" class="headerlink" title="&#x7ED3;&#x8BED;"></a>&#x7ED3;&#x8BED;</h2><p><strong>&#x56FE;&#x6BD4;&#x8F83;&#x4E11;&#xFF0C;&#x8FD8;&#x8BF7;&#x5404;&#x4F4D;&#x89C1;&#x8C05;&#xFF0C;&#x6253;&#x8138;&#x8F7B;&#x70B9;&#x3002;&#x4EE3;&#x7801;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x76F8;&#x4FE1;&#x5404;&#x4F4D;&#x90FD;&#x53EF;&#x4EE5;&#x770B;&#x61C2;,&#x77E5;&#x8BC6;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x8981;&#x591A;&#x5199;&#xFF0C;&#x8FD8;&#x8981;&#x591A;&#x603B;&#x7ED3;&#x3002;</strong></p>
<p>&#x200B;      </p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/21/Android 自定义接口/" rel="next" title="Android 自定义接口">
                <i class="fa fa-chevron-left"></i> Android 自定义接口
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/21/Android-Socket编程（tcp）初探/" rel="prev" title="Android Socket编程（tcp）初探">
                Android Socket编程（tcp）初探 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Silence潇湘夜雨</p>
              <p class="site-description motion-element" itemprop="description">Android开发一枚</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/muyishuangfeng" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/1e2eec6f972c" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/58df0abf61ff4b006b115e43" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-star"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yangke20792452" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#1、首先自定义日期选择工具类"><span class="nav-number"></span> <span class="nav-text">1、首先自定义日期选择工具类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、布局文件"><span class="nav-number"></span> <span class="nav-text">2、布局文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、如何调用"><span class="nav-number"></span> <span class="nav-text">3、如何调用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number"></span> <span class="nav-text">结语</span></a></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Silence潇湘夜雨</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">114.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":false},"react":{"opacityDefault":0.5,"opacityOnHover":0.5},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
